﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DLL;
using Public;

namespace QLBanVe
{
    public partial class frmVeChuyenBay : UserControl
    {
        public frmVeChuyenBay()
        {
            InitializeComponent();
        }
        ChuyenBayDLL cb = new ChuyenBayDLL();
        HangVeDLL hv = new HangVeDLL();
        TrinhTrangVePublic ttp = new TrinhTrangVePublic();
        TrinhTrangVeDLL ttd = new TrinhTrangVeDLL();

        string total = "";
        private void frmVeChuyenBay_Load(object sender, EventArgs e)
        {
            txtSanBayDi.Enabled = false;
            txtSanBayDen.Enabled = false;
            txtNgayGio.Enabled = false;
            txtTrinhTrangVe.Enabled = false;            
            txtGiaTien.Enabled = false;
            txtTongTien.Enabled = false;
            txtTenHanhKhach.Enabled = false;
            txtCMND.Enabled = false;
            txtDienThoai.Enabled = false;
            Loadcb();
            Loadcb1();
            xltextbox();
        }

        void xltextbox()
        {

            txtSanBayDi.Text = "";
            txtSanBayDen.Text = "";
            txtNgayGio.Text = "";
            txtTongTien.Text = "";
            txtTrinhTrangVe.Text = "";
            txtGiaTien.Text = "";
            txtCMND.Text = "";
            txtDienThoai.Text = "";
            txtTenHanhKhach.Text = "";
            
        }
        //Hàm load san bay vao combox
        private void Loadcb()
        {
            
            cbMaChuyenBay.DataSource = cb.LoadChuyenBay();
            cbMaChuyenBay.DisplayMember = "MaChuyenBay";
            cbMaChuyenBay.ValueMember = "MaChuyenBay";
            cbMaChuyenBay.SelectedIndex = -1;
        }
        private void Loadcb1()
        {
            cbHangVe.DataSource = hv.LoadHangVe();
            cbHangVe.DisplayMember = "TenHangVe";
            cbHangVe.ValueMember = "MaHangVe";
            //cbMaChuyenBay.SelectedIndex = -1;
        }
        private void ChonCB()
        {
            DataTable dt = new DataTable();
            if (cbMaChuyenBay.SelectedIndex != -1)
            {
                int i = cbMaChuyenBay.SelectedIndex;
                dt = cb.LoadChuyenBay_Ve();
                DataRow dr;
                dr = dt.Rows[i];
                txtSanBayDi.Text = dr["SanBayDi"].ToString();
                txtSanBayDen.Text = dr["SanBayDen"].ToString();
                txtNgayGio.Text = dr["NgayGio"].ToString();
                txtGiaTien.Text = dr["GiaTien"].ToString();
                total=dr["GiaTien"].ToString();;
                txtTongTien.Text = total;

                //trình trạng trống
                ttp.MaChuyenBay = dr["MaChuyenBay"].ToString();


                DataTable bangb = new DataTable();
                bangb = ttd.TrinhTrangVe_GheTrong(ttp);
                DataRow dongd;
                dongd = bangb.Rows[0];
                int GheTrong = int.Parse(dongd["SoLuongGheTrong"].ToString());

                //Trình trạng ghế đặt

                DataTable bang1 = new DataTable();
                bang1 = ttd.TrinhTrangVe_GheDat(ttp);
                DataRow dong1;
                dong1 = bang1.Rows[0];
                int GheDat = int.Parse(dong1["SoLuongGheDat"].ToString());
                ////////
                if (GheTrong < 0 || GheTrong == 0)
                {
                   
                    txtTrinhTrangVe.Text = "Hết vé";
                }
                else
                    txtTrinhTrangVe.Text = "Còn chỗ";
                
            }

        }


        private void btnMaHanhKhach_Click(object sender, EventArgs e)
        {
            AddKhachHang ad = new AddKhachHang();
            ad.Show();
        }

      

        private void cbMaChuyenBay_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            ChonCB();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            AddKhachHang frm = new AddKhachHang();
            frm.Show();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                int GheDat1 = 0;
                int GheTrong1 =0;

                //update trình trạng vé

                //Kiểm tra trình trạng vé
                if (cbMaChuyenBay.SelectedIndex != -1)//Lấy giá trị từ combox 
                {
                    //trình trạng trống
                    DataTable bang = new DataTable();
                    bang = ttd.TrinhTrangVe_GheTrong(ttp);
                    DataRow dong;
                    dong = bang.Rows[0];
                    GheTrong1 = int.Parse(dong["SoLuongGheTrong"].ToString());//SỐ ghế trống

                   
                    //Trình trạng ghế đặt
                    DataTable bang1 = new DataTable();
                    bang1 = ttd.TrinhTrangVe_GheDat(ttp);
                    DataRow dong1;
                    dong1 = bang1.Rows[0];
                    GheDat1 = int.Parse(dong1["SoLuongGheDat"].ToString());//số ghế đặt

                    
                    DataTable dt = new DataTable();
                    GheDat1 = GheDat1 + 1;
                    GheTrong1 = GheTrong1 - 1;
                    
                    if (GheTrong1 < 0)
                    {
                        MessageBox.Show("Đã hết vé");
                    }
                    else
                    {
                        //Cập nhật trình trạng vé
                        int i = cbMaChuyenBay.SelectedIndex;
                        dt = cb.LoadChuyenBay();
                        DataRow dr;
                        dr = dt.Rows[i];
                        ttp.SoLuongGheTrong = GheTrong1;
                        ttp.SoLuongGheDat = GheDat1;
                        ttp.MaChuyenBay = dr["MaChuyenBay"].ToString();

                        ttd.TrinhTrangVe_update(ttp);
                        //////////////

                        VeChuyenBayDLL vcbdll = new VeChuyenBayDLL();
                        VeChuyenBayPublic vcbp = new VeChuyenBayPublic();
                       
                        if (cbMaChuyenBay.SelectedIndex != -1)//Lấy giá trị từ combox 
                        {
                            vcbp.MaChuyenBay = cbMaChuyenBay.SelectedValue.ToString();

                        }
                        vcbp.MaHanhKhach = txtMaKhachHang.Text;
                        if (cbHangVe.SelectedIndex != -1)//Lấy giá trị từ combox hạng vé
                        {
                            vcbp.MaHangVe = cbHangVe.SelectedValue.ToString();
                        }

                        vcbp.GiaTien = txtTongTien.Text;
                        int ai = vcbdll.VeChuyenBay_Insert(vcbp);

                        if (ai > 0)
                        {
                            cbMaChuyenBay.SelectedIndex = -1;
                            txtMaKhachHang.Text = "";
                            xltextbox();
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Thất bại" + ex);
            }
        }

        private void txtMaKhachHang_TextChanged(object sender, EventArgs e)
        {
            KhachHangDLL khdll = new KhachHangDLL();
            KhachHangPublic khp = new KhachHangPublic();
            khp.MaKhachHang = txtMaKhachHang.Text;
            DataTable dt = khdll.Timkiem(khp);


            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {

                    txtTenHanhKhach.Text = dr["TenKhachHang"].ToString();
                    txtDienThoai.Text = dr["DienThoai"].ToString();
                    txtCMND.Text = dr["CMND"].ToString();
                    txtDienThoai.Text = dr["DienThoai"].ToString();
                    button1.Enabled = false;

                }
            }

            else
            {
                button1.Enabled = true;
                txtTenHanhKhach.Text = "";
                txtCMND.Text = "";
                txtDienThoai.Text = "";
            }
 
        }
      
    }       
}
